package zozopo.search;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.apache.struts2.interceptor.ServletRequestAware;

import zozopo.search.Searcher;
import zozopo.search.SearcherBean;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

import org.apache.struts2.ServletActionContext;

public class SearcherAction extends ActionSupport implements Action,ServletRequestAware{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	private static int TOP_NUM = 100;//显示前100条结果   
	private String searchstr;
	private String search;
	private int pageSize = 20;
	private int beginPageNum = 0;
	private int countRow = 0;
	private int countPage = 1;
	private String searchInfo;
	private ArrayList<SearcherBean> resultList;

	public int getCountPage() {
		return countPage;
	}

	public void setCountPage(int countPage) {
		this.countPage = countPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getBeginPageNum() {
		return beginPageNum;
	}





	public void setBeginPageNum(int beginPageNum) {
		this.beginPageNum = beginPageNum;
	}





	public int getCountRow() {
		return countRow;
	}

	public void setCountRow(int countRow) {
		this.countRow = countRow;
	}





	public String getSearchstr() {
		return searchstr;
	}





	public void setSearchstr(String searchstr) throws UnsupportedEncodingException {
		this.searchstr = java.net.URLDecoder.decode(searchstr, "UTF-8");
		
	}


	
	
	
	
	public ArrayList<SearcherBean> getResultList() {
		return resultList;
	}





	public void setResultList(ArrayList<SearcherBean> resultList) {
		this.resultList = resultList;
	}





	private File indexDir;
	
	public String execute() throws Exception{
		
			indexDir = new File(ServletActionContext.getServletContext().getRealPath("/search/index"));
			Searcher searcher = new Searcher();
		 	
			//searchstr = searchstr.replace(" ", "");
		   
		  
		  
		       
		    long start = new Date().getTime();// start time   
		       
		    
		  //  ArrayList<String> idList = searcher.executeSearchForIdList(indexDir, "", searchstr);
		  
		  //  System.out.println(idList.size());
		    

		    
		    long end = new Date().getTime();//end time    
		    
		    
		    resultList = searcher.selectSearchList(indexDir, "", searchstr,beginPageNum,pageSize);
		    searcher.releaseConnection();
		    searchInfo = "找到相关页面 " + resultList.size() +   
		              " 篇 (用时 " + (end - start) +   
		              " 毫秒) 匹配 '" +   
		              searchstr + "'";
		    
		    countRow = searcher.getSearcherRow(indexDir, "", searchstr);
		    countPage =(countRow-1)/pageSize+1;
		    
			
			
		return SUCCESS;
	}
	
	public void validate() {
		// TODO Auto-generated method stub
		if(search == null){
			addFieldError("errormessage", "搜索内容不能为空");
			return;
		}
		
		if(search.equals("")){
			addFieldError("errormessage", "搜索内容不能为空");
			return;
		}//*/
	}
	
	
	
	
	public String getSearch() {
		return search;
	}

	public void setSearch(String search) throws UnsupportedEncodingException {
		this.searchstr = java.net.URLDecoder.decode(search, "UTF-8");;
		this.search = search;
	}

	public String getSearchStr() {
		
		return searchstr;
	}


	public void setSearchStr(String searchStr) throws UnsupportedEncodingException {
		this.search = searchStr;
		this.searchstr = java.net.URLDecoder.decode(searchStr, "UTF-8");;
	}





	public String getSearchInfo() {
		return searchInfo;
	}





	public void setSearchInfo(String searchInfo) {
		this.searchInfo = searchInfo;
	}



	HttpServletRequest request;
	
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		request = arg0;
		
	}
}
